﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class UserDao : DataAccessObject
    {
        #region Get

        public List<User> GetFinanciadorUsers(int financiadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.user
                                where (e.financiadorId == financiadorId)
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public User GetUser(string username, string password, int financiadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.user.FirstOrDefault(e =>
                        e.userName.CompareTo(username) == 0 &&
                        e.password.CompareTo(password) == 0 &&
                        e.financiadorId == financiadorId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public User GetUser(int userId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.user.FirstOrDefault(e =>
                        e.userId == userId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Add

        public void AddUser(User user)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddTouser(user);
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void UpdateUserLoginData(int userId, DateTime? loginDate, bool loggedIn)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    User user = entities.user.FirstOrDefault(e => e.userId == userId);
                    user.lastLoginDate = loginDate.HasValue ? loginDate : user.lastLoginDate;
                    user.loggedIn = loggedIn;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public void ModifyUser(User user)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    User oldUser = entities.user.FirstOrDefault(e => e.userId == user.userId);
                    oldUser = user;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
